<%-- 
    Document   : createInvoice
    Created on : Jan 2, 2010, 11:27:00 AM
    Author     : Administrator
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib  prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">
<table border="1">
    <tr>
        <td rowspan="3">
            <table border="1">
                <tr>
                    <td>
                        <strong>Customer: </strong>
                    </td>
                    <td>
                        <c:choose>
                            <c:when test="${invoice.customer!=null}">
                                <a href="<%=request.getContextPath()%>/customer/view?id=${invoice.customer.autoId}" target="_blank">${invoice.customer.name}</a>
                            </c:when>
                            <c:otherwise>
                                (None)
                            </c:otherwise>
                        </c:choose>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <form method="post">
                            Search:
                            <input type="text" value='${customerKey}' name="customerKey"/>
                            <input type="submit" name="findCustomer" value="Find" />
                        </form>
                    </td>
                </tr>
                <c:if test="${suggestedCustomers!=null}">
                    <c:forEach var="customer" items="${suggestedCustomers}">
                        <form method="post">
                            <tr>
                                <td>
                                    ${customer.personalId} - ${customer.name}
                                </td>
                                <td>
                                    <input type="hidden" name="customerId" value="${customer.autoId}"/>
                                    <input type="submit" name="selectCustomer" value="Select" />
                                </td>
                            </tr>
                        </form>
                    </c:forEach>
                </c:if>
            </table>
        </td>
        <td width="78">
            <strong>No.</strong>
        </td>
        <td width="106">
            ${invoice.number}
        </td>
    </tr>
    <tr>
        <td>
            <strong>Date:</strong>
        </td>
        <td>
            <fmt:formatDate value="${invoice.createdDate}" pattern="MMM d, yyyy"/>
        </td>
    </tr>
    <tr>
        <td>
            <strong>Saler ID: </strong>
        </td>
        <td>
            --
        </td>
    </tr>
</table>
<table border="1">
    <tr>
        <td>
            <strong>No.</strong>
        </td>
        <td>
            <strong>Product Code</strong>
        </td>
        <td>
            <strong>Product Name</strong>
        </td>
        <td>
            <strong>Quantity</strong>
        </td>
        <td>
            <strong>Price</strong>
        </td>
        <td>
            <strong>Sub Total</strong>
        </td>
    </tr>
    <form method="post">
        <input type="hidden" name="removeInvoiceItemId" id="removeInvoiceItemId"/>
        <c:set var="total" value="0"/>
        <c:forEach var="invoiceItem" items="${invoice.invoiceItems}">
            <tr>
                <td>
                    ${counter.count}
                </td>
                <td>
                    ${invoiceItem.product.code}
                </td>
                <td>
                    ${invoiceItem.product.name}
                </td>
                <td>
                    <input type="text" size="4" name="iq_${invoiceItem.autoId}" value="${invoiceItem.quantity}" />
                    (${invoiceItem.product.unit})
                </td>
                <td>
                    x ${invoiceItem.price} VND
                </td>
                <td>
                    ${invoiceItem.price * invoiceItem.quantity} VND
                    <c:set var="total" value="${total + invoiceItem.price * invoiceItem.quantity}"/>
                </td>
                <td>
                    <input type="submit" onclick="document.getElementById('removeInvoiceItemId').value = '${invoiceItem.autoId}';" name="removeItem" value="Remove"/>
                </td>
            </tr>
        </c:forEach>
        <tr>
            <td>
            </td>
            <td>
            </td>
            <td>
            </td>
            <td>
                <input type="submit" name="updateQuantity" value="Update" />
            </td>
            <td>
            </td>
            <td>
            </td>
        </tr>
    </form>
    <tr>
        <td colspan="4" align="right">
            Total
        </td>
        <td colspan="2">
            <strong>${total}</strong>
        </td>
    </tr>
    <tr>
        <td colspan="6">
            <form method="post">
                Search for product:
                <input type="text" name="productKey" value="${productKey}" />
                <input type="submit" name="findProduct" value="Find" />
            </form>
        </td>
    </tr>
    <c:if test="${suggestedProducts!=null}">
        <c:forEach var="product" items="${suggestedProducts}">
            <form method="post">
                <tr>
                    <td>
                    </td>
                    <td>
                        ${product.code}
                    </td>
                    <td>
                        ${product.name}
                    </td>
                    <td>
                        <input type="text" size="4" name="quantity" value="1" />
                    </td>
                    <td>
                        ${product.currentSellingPrice} VND
                    </td>
                    <td>
                        <input type="hidden" name="productId" value="${product.autoId}"/>
                        <input type="submit" name="insertProduct" value="Insert" />
                    </td>
                </tr>
            </form>
        </c:forEach>
    </c:if>
    <tr>
        <td colspan="6" align="center">
            <form method="post">
                <input type="submit" name="cancelInvoice" value="Cancel Invoice" onclick="return confirm('Cancel this invoice?')" />
                <input type="submit" name="saveInvoice" value="Save Invoice" />
            </form>
        </td>
    </tr>
</table>
